import array from "../../../models/comm/array.js"  //数组操作模块
import task from "../../../models/dict/task.js"  //数组操作模块
import flow from "../../../models/dict/flow.js"  //数组操作模块
import moment from 'moment';
var py = require("../../../models/comm/webPy");
import axios from 'axios';
import Vue from 'vue'

import taskInfo from '../task/taskInfo.vue';
export default {


    data: function () {


        return {
            taskClassArray: task.classArray(),
            flowArray: [],
            flowSelList: [],

            task: {},   //当前选择的任务
            showTaskInfo: false

        }
    },
    props: [

        "currObj",
        "currNodeObj",
        "s",
        "customerOrder",
        "taskList"






    ],
    methods: {

        //执行当前
        exeCustomerOrderFlow: function (node) {

            this.$emit("exeCustomerOrderFlow", node)
        },

        toSaveNode: function (node) {

            if (!node.managers) {
                Vue.set(node, 'managers', [])
                //  this.$set(this.currNodeObj, "managers",[{ name:"123",_id:"123" }]);

            }

            if (!node.members) {
                Vue.set(node, 'members', [])
                //  this.$set(this.currNodeObj, "managers",[{ name:"123",_id:"123" }]);

            }
            if (!node.time) {

                Vue.set(node, 'time', [])
                //  console.log("没有时间啊");
            }
            this.$emit("toSaveNode", node)

        },


        myRely: function (node) {
            //  console.log(JSON.stringify(node));

            var array = [];
            if (node.rely) {

                node.rely.forEach(function (relyId) {
                    this.currObj.nodeList.forEach(function (node) {
                        if (relyId == node.id) {
                            array.push({

                                id: relyId,
                                name: node.name
                            })

                        }

                    }, this);

                }, this);
            }

            return array;
        },
        nodeRemove: function (index) {

            var n = this.currObj.nodeList[index];
            if (n.id == "root") {

                this.$message({
                    message: '起始节点暂时不允许删除',
                    type: 'warning'
                });
                return;
            }
            else {

                this.currObj.nodeList.splice(index, 1)
            }

        },
        nodeSort: function (i, index) {
            var n = this.currObj.nodeList[index];
            var n2 = this.currObj.nodeList[index + i];

            if (n.id == 'root' || n2.id == 'root') {
                //不能与起始节点互换
                this.$message({
                    message: '不能与起始节点互换',
                    type: 'warning'
                });
            }
            else {

                if (n2) {
                    this.currObj.nodeList[index] = n2;
                    this.currObj.nodeList[index + i] = n;
                    this.currObj.nodeList = array.clone(this.currObj.nodeList);
                }
                else {

                }
            }


            //   console.log(i); console.log(index);
        },


        myTaskClassName: function (node) {
            var taskClassName = "";
            this.taskClassArray.forEach(function (taskClass) {
                if (taskClass.id == node.taskClassId) {

                    taskClassName = taskClass.name;

                    return false;
                }



            }, this);
            return taskClassName;
        },
        addNode: function () {

            // 这里不是this
            var currNodeObj = {
                id: moment().utcOffset(-8).format("YYMMDDHHmmssSSS"),
                name: "新节点",
                taskClassId: "default",
                manager: [],
                fatherNodeId: "root",
                rely: []   //依赖节点的ID, 

            }

            this.toSaveNode(currNodeObj);

        },
        queryFlowList: async function (query, cb) {




            var data = {
                "$or": [
                    { name: { $regex: query } },
                    { namePy: { $regex: await py.firstLetter(query) } }
                ]
            }
            // console.log(JSON.stringify(data));
            if (query !== '') {
                this.loading = true;
                axios.post("/a", {
                    class: "flow",
                    para: "getFlowList",
                    j: {
                        data: data

                    }

                }).then((res) => {

                    this.loading = false;
                    this.flowSelList = res.data.list;

                    this.flowSelList.forEach(function (flow) {
                        flow.value = flow.name;

                    }, this);
                    cb(this.flowSelList)

                });

            }
        },

        selFlow: function (flow) {
            var _this = this;
            setTimeout(function () {
                _this.$confirm('此操作将覆盖当前流程, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    _this.currObj.name = flow.value;




                    Vue.set(_this.currObj, "nodeList", _this.formatNodeList(flow.nodeList))

                    _this.$message({
                        type: 'success',
                        message: '覆盖成功!'
                    });
                }).catch(() => {
                    _this.$message({
                        type: 'info',
                        message: '已取消覆盖.'
                    });
                });
            }, 500);






        },
        getNodeStatus: function (statusId) {
            if (!flow.getNodeStatus(statusId)) {
                return {};
            }
            else {
                return flow.getNodeStatus(statusId)
            }


        },
        formatNodeList: function (nodeList) {

            nodeList.forEach(function (node, index) {
                var oldnodeId = node.id;
                node.id = moment().utcOffset(-8).format("YYMMDDHHmmssSSS") + "_" + index.toString()

                nodeList.forEach(function (node2) {

                    node2.rely.forEach(function (relyNodeId, index2) {
                        if (relyNodeId == oldnodeId) {
                            node2.rely[index2] = node.id
                        }
                    }, this);
                }, this);
            }, this);

            return nodeList
        },
        test(a, b, c) {

            console.log(a);

        },
        loadPage() {

            console.log("加载flowNodeList");


        },
        toSaveTask(node) {
            this.task = null


            this.taskList.forEach(function (_task) {

                if (node.id == _task.nodeId) {
                    this.task = _task
                }
            }, this);

            if (!this.task) {
                this.$message({
                    message: '此节点没有任务!',
                    type: 'error'  //success  warning  info  error
                });
            }
            else {
                this.$emit('toSaveTask', this.task);
            }

        },
        getMembers(type, node) {

            if (this.s == "customerOrder") {

                if (node.statusId >= 100) {
                    var myTask = null;
                    this.taskList.forEach(function (task) {
                        if (task.nodeId == node.id) {
                            myTask = task
                        }

                    }, this);
                    if (myTask) {   //如果有任务/返回任务的
                        return myTask[type]
                    }
                    else {
                        return node[type]
                    }
                }
                else {
                    return node[type]
                }


            }
            else {
                return node[type]
            }

        }

    },
    mounted() {
        this.loadPage()
    },
    watch: {

    },
    components: {
        taskInfo
    }

}